using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(ControlTypeDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:ControlTypeDataSource runat=server/>")]
    public class ControlTypeDataSource : GenericDataSource<Bll.ControlType>
    {
        public enum Fields
        {
            Id,
            SiteId,
            Name,
            AdminControlType,
            AdminDisplayControlType,
            AdminEditControlType,
            FrontEndControlType,
            Custom,
            Notes,
            ConcurencyId
        }

        protected override Acms.Bll.ControlType PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Bll.ControlType tmp = null;
            byte[] concurencyId = null;

            if (action == UpdateAction.Insert)
            {
                tmp = Bll.ControlType.CreateInstance();
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = Bll.ControlType.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);

                if (RequiresUpdate<int>(Fields.SiteId, values, oldValues))
                {
                    tmp.SiteId = GetValue<int>(Fields.SiteId, values);
                }

                if (RequiresUpdate<string>(Fields.Name, values, oldValues))
                {
                    tmp.Name = GetValue<string>(Fields.Name, values);
                }

                if (RequiresUpdate<string>(Fields.AdminControlType, values, oldValues))
                {
                    tmp.AdminControlType = GetValue<string>(Fields.AdminControlType, values);
                }

                if (RequiresUpdate<string>(Fields.AdminDisplayControlType, values, oldValues))
                {
                    tmp.AdminDisplayControlType = GetValue<string>(Fields.AdminDisplayControlType, values);
                }

                if (RequiresUpdate<string>(Fields.AdminEditControlType, values, oldValues))
                {
                    tmp.AdminEditControlType = GetValue<string>(Fields.AdminEditControlType, values);
                }

                if (RequiresUpdate<string>(Fields.FrontEndControlType, values, oldValues))
                {
                    tmp.FrontEndControlType = GetValue<string>(Fields.FrontEndControlType, values);
                }

                if (RequiresUpdate<bool>(Fields.Custom, values, oldValues))
                {
                    tmp.Custom = GetValue<bool>(Fields.Custom, values);
                }

                if (RequiresUpdate<string>(Fields.Notes, values, oldValues))
                {
                    tmp.Notes = GetValue<string>(Fields.Notes, values);
                }
            }
            return tmp;
        }

        protected override int SaveChanges(Acms.Bll.ControlType item)
        {
            item.Update();
            return 1;
        }

        protected override void AddInsertedKey(Acms.Bll.ControlType item, System.Collections.IDictionary values)
        {
            values[Fields.Id.ToString()] = item.Id;
        }

        protected override int Delete(Acms.Bll.ControlType item)
        {
            item.Delete();
            return 1;
        }
    }

    public class ControlTypeDataSourceDesigner : GenericDataSourceDesigner<Bll.ControlType>
    {
        protected override Acms.Bll.ControlType GetDesignTimeItem(int index)
        {
            string[] names = new string[]{"Text", "Resource", "Html", "Posting"};
            Random rnd = new Random(index);
            Bll.ControlType tmp = Bll.ControlType.CreateInstance();
            tmp.SiteId = rnd.Next(1,10);
            tmp.Name = names[rnd.Next(0, names.Length)];
            tmp.Custom = rnd.Next(0, 1) == 1;
            return tmp;
        }
    }
}
